package com.letwal.cmcvnd.create.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.RevDataMnemonic

class MnemonicVerifyStatusAdapter(val dataList: List<RevDataMnemonic>) :
    RecyclerView.Adapter<MnemonicVerifyStatusAdapter.ViewHolder>() {

    //点击事件
    private var mItemClickListener: (Int) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAdpterMnemonicVerifyStatusContent: RelativeLayout =
            view.findViewById(R.id.rlAdpterMnemonicVerifyStatusContent)

        var textAdpterMnemonicVerifyStatus: TextView =
            view.findViewById(R.id.textAdpterMnemonicVerifyStatus)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_mnemonic_verify_status, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataMnemonic = dataList[position]
        holder.textAdpterMnemonicVerifyStatus.text = data.content

        val context = holder.rlAdpterMnemonicVerifyStatusContent.context
        if (data.selected) {
            holder.rlAdpterMnemonicVerifyStatusContent.setBackgroundResource(R.drawable.mnemonic_card_selected)
            holder.textAdpterMnemonicVerifyStatus.setTextColor(
                ContextCompat.getColor(
                    context,
                    R.color.white
                )
            )

        } else {
            holder.rlAdpterMnemonicVerifyStatusContent.setBackgroundResource(R.drawable.mnemonic_card)
            holder.textAdpterMnemonicVerifyStatus.setTextColor(
                ContextCompat.getColor(
                    context,
                    R.color.main_black
                )
            )
        }

        //设置点击事件
        holder.rlAdpterMnemonicVerifyStatusContent.setOnClickListener {
            mItemClickListener.invoke(position)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (Int) -> Unit) {
        mItemClickListener = listener
    }
}